#pragma	once

#include <assert.h>

namespace	Z	{

//------------------------------------------------------------------------------------------------------------------------
struct	StreamException
{
};

//------------------------------------------------------------------------------------------------------------------------
class	WriteStreamVerifier
{
public:
	template<typename T>	static	void	verify(T& buffer, size_t kSize)	{
		if(kSize <= buffer.getWritableSize())
			return;
		buffer.mStreamBuf->reallocate(getRequiredSize(buffer, kSize));
	}
private:
	template<typename T>	static	size_t	getRequiredSize(const T& buffer, size_t kSize)	{
		assert(buffer.getWritableSize() < kSize);
		size_t extendedSize	= buffer.mStreamBuf->getInitialAllocSize();
		size_t curWritableSize	= buffer.getWritableSize();
		while(curWritableSize < kSize)	{
			extendedSize	*= 2;
			curWritableSize	= extendedSize - buffer.mStreamBuf->getAllocSize() + buffer.getWritableSize();
		}
		return extendedSize;
	}
};

//------------------------------------------------------------------------------------------------------------------------
class	ReadStreamVerifier
{
public:
	template<typename T>	static	void	verify(T& buffer, size_t kSize)	{
		if(buffer.getReadableSize() < kSize)	{
			throw StreamException();
		}
	}
};

}